/*
 * @Author: wurangkun wurangkun@qq.com
 * @Date: 2023-07-07 12:01:48
 * @LastEditors: wurangkun wurangkun@qq.com
 * @LastEditTime: 2023-07-12 14:17:35
 * @FilePath: \automatic-shutdown\source\app.js
 */
const { count_down } = require("./module/count_down");
const notifier = require('node-notifier');
const fs = require('fs')
const path = require('path')
const filePath = path.join(__dirname, './module/data/pid.text');
const timePath = path.join(__dirname, './module/data/time.text');
const datePath = path.join(__dirname, './module/data/date.text');
const date = fs.readFileSync(datePath).toString();
// 创建当前日期对象
const today = new Date();

// 创建昨天日期对象
const yesterday = new Date(+date);

// 获取当前日期的年、月、日
const currentYear = today.getFullYear();
const currentMonth = today.getMonth() + 1; // 注意，JavaScript 中的月份是从 0 开始计数的，所以需要加 1
const currentDay = today.getDate();

// 获取昨天的日期的年、月、日
const previousYear = yesterday.getFullYear();
const previousMonth = yesterday.getMonth() + 1;
const previousDay = yesterday.getDate();

if (date) {
  // 比较当前日期和昨天的日期
  if (currentYear > previousYear || currentMonth > previousMonth || currentDay > previousDay) {
    console.log("已经过了一天，现在是第二天。");
    fs.writeFileSync(timePath, '0')
    fs.writeFileSync(datePath, '' + Date.now())
  }
} else {
  fs.writeFileSync(datePath, '' + Date.now())
}
const pid = fs.readFileSync(filePath).toString()
console.log(process);
if (!pid) {
  fs.writeFileSync(filePath, process.pid + '')
  console.log('当前 Node.js 进程的PID:', process.pid);
  count_down()
} else {
  notifier.notify({
    title: '提示',
    message: `自动关机程序已在运行`,
    function(error, response) {
      console.log(response, error);
    }
  });
}